﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.Utility;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;

namespace BCGC.BLL.BusinessObject.Resource
{
    public class WallpaperSize
    {
        private int wallpaperSizeId;
        public int WallpaperSizeId
        {
            get { return wallpaperSizeId; }
            private set { wallpaperSizeId = value; }
        }

        private int wallpaperId;
        public int WallpaperId
        {
            get { return wallpaperId; }
            private set { wallpaperId = value; }
        }

        private string sizeName;
        public string SizeName
        {
            get { return sizeName; }
            set { sizeName = value; }
        }

        private string sizeFileUrl;
        public string SizeFileUrl
        {
            get { return sizeFileUrl; }
            set { sizeFileUrl = value; }
        }

        public WallpaperSize()
        {
            this.wallpaperId = 0;
            this.wallpaperSizeId = 0;
            this.sizeFileUrl = string.Empty;
            this.sizeName = string.Empty;
        }

        public WallpaperSize(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("WallpaperSizeId"))
                    this.wallpaperSizeId = (int)CommonUtility.FilterNull(dataRow["WallpaperSizeId"], typeof(int));
                if (dataRow.Table.Columns.Contains("WallpaperId"))
                    this.wallpaperId = (int)CommonUtility.FilterNull(dataRow["WallpaperId"], typeof(int));
                if (dataRow.Table.Columns.Contains("SizeName"))
                    this.sizeName = (string)CommonUtility.FilterNull(dataRow["SizeName"], typeof(string));
                if (dataRow.Table.Columns.Contains("SizeFileUrl"))
                    this.sizeFileUrl = (string)CommonUtility.FilterNull(dataRow["SizeFileUrl"], typeof(string));
            }
            else
            {
                this.wallpaperId = 0;
                this.wallpaperSizeId = 0;
                this.sizeFileUrl = string.Empty;
                this.sizeName = string.Empty;
            }
        }

        private static ReadWriteCustomCollection<WallpaperSize> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<WallpaperSize> result = new ReadWriteCustomCollection<WallpaperSize>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new WallpaperSize(row));
                }
            }
            return result;
        }

        public static ReadWriteCustomCollection<WallpaperSize> GetAllWallpaperSizesByWallpaperId(int wallpaperId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            ReadWriteCustomCollection<WallpaperSize> wallpaperSizes = null;
            DataTable data = ResourceData.GetWallpaperSizesByWallpaperId(wallpaperId);
            if (data != null && data.Rows.Count > 0)
                wallpaperSizes = LoadFromDataTable(data);

            DataFactory.GetDataMiner().CommitTransaction();

            return wallpaperSizes;
        }

        public static bool Create(int wallpaperId, string sizeName, string sizeFileUrl)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = ResourceData.CreateNewWallpaperSize(wallpaperId, sizeName, sizeFileUrl);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static WallpaperSize GetWallpaperSizeByWallpaperSizeId(int wallpaperSizeId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            WallpaperSize wallpaperSize = null;
            DataTable data = ResourceData.GetWallpaperSizeById(wallpaperSizeId);
            if (data != null && data.Rows.Count > 0)
                wallpaperSize = new WallpaperSize(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return wallpaperSize;
        }

        public static bool Update(int wallpaperSizeId, string sizeName, string sizeFileUrl)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            List<int> wallpaperSizesToDelete = new List<int>();
            wallpaperSizesToDelete.Add(wallpaperSizeId);
            DataTable wallpaperSizeFiles = ResourceData.GetWallpaperSizeFileLocationsForDelete(wallpaperSizesToDelete);
            DataFactory.GetDataMiner().CommitTransaction();

            if (wallpaperSizeFiles != null && wallpaperSizeFiles.Rows.Count > 0)
            {
                foreach (DataRow row in wallpaperSizeFiles.Rows)
                {
                    string oldFileUrl = Convert.ToString(row["SizeFileUrl"]);
                    if (!string.IsNullOrEmpty(oldFileUrl))
                        FileAccessUtility.DeleteFile(oldFileUrl);
                }
            }

            DataFactory.GetDataMiner().BeginTransaction(true);
            int updateCount = ResourceData.UpdateWallpaperSize(wallpaperSizeId, sizeName, sizeFileUrl);
            DataFactory.GetDataMiner().CommitTransaction();

            return (updateCount > 0);
        }

        public static bool AllreadySizeExists(string sizeName)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            int count = ResourceData.GetWallpaperSizeCountBySizeName(sizeName);

            DataFactory.GetDataMiner().CommitTransaction();

            return (count > 0);
        }

        public static bool Delete(List<int> wallpaperSizeIdsToDelete)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable wallpaperSizeFiles = ResourceData.GetWallpaperSizeFileLocationsForDelete(wallpaperSizeIdsToDelete);
            DataFactory.GetDataMiner().CommitTransaction();

            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int deletedItemCount = ResourceData.DeleteWallpaperSizes(wallpaperSizeIdsToDelete);
                if (deletedItemCount == wallpaperSizeIdsToDelete.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to delete all items");

                if (wallpaperSizeFiles != null && wallpaperSizeFiles.Rows.Count > 0)
                {
                    foreach (DataRow row in wallpaperSizeFiles.Rows)
                    {
                        string fileUrl = Convert.ToString(row["SizeFileUrl"]);
                        if (!string.IsNullOrEmpty(fileUrl))
                            FileAccessUtility.DeleteFile(fileUrl);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }
    }
}
